#include "user_draw_wave.h"
// 信号类型枚举
// 信号波形绘制系统
// 适用于STM32F407 + U8G2库 + 128x64 OLED屏幕
// 显示区域配置

// ==================== 全局变量 ====================

static display_area_t g_display_area = {
    .x_start = 0,
    .y_start = 16,     // 为参数显示预留上方空间
    .width = 128,
    .height = 48,      // 为参数显示预留下方空间
    .grid_enabled = 1
};

// ==================== 核心函数声明 ====================

void signal_display_init(u8g2_t *u8g2);
void signal_display_update(u8g2_t *u8g2, wave_t *signal);
static signal_type_t detect_signal_type(wave_t *signal);
static void calculate_wave_params(wave_t *signal, wave_params_t *params);
static void draw_coordinate_system(u8g2_t *u8g2);
static void draw_signal_waveform(u8g2_t *u8g2, wave_t *signal, const wave_params_t *params);
static void draw_signal_info(u8g2_t *u8g2, wave_t *signal);
static float generate_wave_point(signal_type_t type, float phase);
//void signal_display_spectrum(u8g2_t *u8g2, wave_t *signal);
// ==================== 主要接口函数 ====================

/**
 * @brief 初始化信号显示系统
 * @param u8g2 U8G2显示器对象指针
 */
void signal_display_init(u8g2_t *u8g2)
{
    // 设置字体
    
    // 清屏
    u8g2_ClearBuffer(u8g2);
    u8g2_DrawStr(u8g2, 0, 10, "Signal Display Ready");
    u8g2_SendBuffer(u8g2);
}

/**
 * @brief 更新显示内容
 * @param u8g2 U8G2显示器对象指针
 * @param signal 信号数据指针
 */
void signal_display_update(u8g2_t *u8g2, wave_t *signal)
{
    if (!signal || signal->wave_freq <= 0) {
        return;
    }
    
    wave_params_t wave_params;

    wave_params.points_count = 200;
    wave_params.time_per_pixel = 0.01;
    wave_params.volt_per_pixel = 0.1;
    wave_params.y_offset = 0;
    // 检测信号类型
    signal->wave_type = SIGNAL_TYPE_SAWTOOTH;
    
    // 计算波形参数
    calculate_wave_params(signal, &wave_params);
    
    // 清屏并开始绘制
    u8g2_ClearBuffer(u8g2);
    
    // 绘制坐标系和网格
    draw_coordinate_system(u8g2);
    
    // 绘制波形
    draw_signal_waveform(u8g2, signal, &wave_params);
    
    // 绘制信号信息
    draw_signal_info(u8g2, signal);
    
    // 发送到显示器
    u8g2_SendBuffer(u8g2);
}

// ==================== 信号类型检测 ====================

// ==================== 波形参数计算 ====================

/**
 * @brief 计算波形绘制参数
 * @param signal 信号数据指针
 * @param params 输出的波形参数指针
 */
static void calculate_wave_params(wave_t *signal, wave_params_t *params)
{
    // 计算合适的时间尺度 - 显示2-3个周期
    float period = 1.0f / signal->wave_freq;
    float total_time = period * 2.5f;  // 显示2.5个周期
    
    params->time_per_pixel = total_time / g_display_area.width;
    
    // 计算合适的电压尺度 - 峰峰值占据80%的显示高度
    float usable_height = g_display_area.height * 0.8f;
    params->volt_per_pixel = signal->wave_peak / usable_height;
    
    // 计算Y轴零点位置
    params->y_offset = g_display_area.y_start + g_display_area.height / 2;
    
    // 设置绘制点数
    params->points_count = g_display_area.width;
}

// ==================== 绘制函数 ====================

/**
 * @brief 绘制坐标系和网格
 * @param u8g2 U8G2显示器对象指针
 */
static void draw_coordinate_system(u8g2_t *u8g2)
{
    uint8_t x_start = g_display_area.x_start;
    uint8_t y_start = g_display_area.y_start;
    uint8_t width = g_display_area.width;
    uint8_t height = g_display_area.height;
    
    // 绘制边框
    u8g2_DrawFrame(u8g2, x_start, y_start, width, height);
    
    if (g_display_area.grid_enabled) {
        // 绘制网格线 - 垂直线
        for (uint8_t i = 1; i < 4; i++) {
            uint8_t x = x_start + (width * i) / 4;
            u8g2_DrawVLine(u8g2, x, y_start + 1, height - 2);
        }
        
        // 绘制网格线 - 水平线
        for (uint8_t i = 1; i < 4; i++) {
            uint8_t y = y_start + (height * i) / 4;
            u8g2_DrawHLine(u8g2, x_start + 1, y, width - 2);
        }
        
        // 绘制中心十字线（加粗）
        uint8_t center_x = x_start + width / 2;
        uint8_t center_y = y_start + height / 2;
        u8g2_DrawVLine(u8g2, center_x, y_start + 1, height - 2);
        u8g2_DrawHLine(u8g2, x_start + 1, center_y, width - 2);
    }
}

/**
 * @brief 绘制信号波形
 * @param u8g2 U8G2显示器对象指针
 * @param signal 信号数据指针
 * @param params 波形参数指针
 */
static void draw_signal_waveform(u8g2_t *u8g2, wave_t *signal, const wave_params_t *params)
{
    int16_t prev_x = -1, prev_y = -1;
    
    for (uint8_t i = 0; i < params->points_count; i++) {
        // 计算当前时间点
        float time = i * params->time_per_pixel;
        
        // 计算相位
        float phase = 2.0f * M_PI * signal->wave_freq * time;
        
        // 根据信号类型生成波形点
        float amplitude = generate_wave_point(signal->wave_type, phase);
        
        // 将幅度转换为屏幕坐标
        float voltage = amplitude * (signal->wave_peak / 2.0f);
        int16_t y = params->y_offset - (int16_t)(voltage / params->volt_per_pixel);
        
        // 限制Y坐标范围
        if (y < g_display_area.y_start + 1) {
            y = g_display_area.y_start + 1;
        }
        if (y > g_display_area.y_start + g_display_area.height - 2) {
            y = g_display_area.y_start + g_display_area.height - 2;
        }
        
        int16_t x = g_display_area.x_start + i;
        
        // 绘制点或线段
        if (prev_x >= 0 && prev_y >= 0) {
            u8g2_DrawLine(u8g2, prev_x, prev_y, x, y);
        } else {
            u8g2_DrawPixel(u8g2, x, y);
        }
        
        prev_x = x;
        prev_y = y;
    }
}

/**
 * @brief 根据信号类型生成波形点
 * @param type 信号类型
 * @param phase 相位 (弧度)
 * @return 归一化幅度值 (-1.0 到 1.0)
 */
static float generate_wave_point(signal_type_t type, float phase)
{
    // 将相位标准化到 [0, 2π] 范围
    while (phase > 2.0f * M_PI) {
        phase -= 2.0f * M_PI;
    }
    while (phase < 0) {
        phase += 2.0f * M_PI;
    }
    
    switch (type) {
        case SIGNAL_TYPE_SINE:
            return sinf(phase);
            
        case SIGNAL_TYPE_SQUARE:
            return (phase < M_PI) ? 1.0f : -1.0f;
            
        case SIGNAL_TYPE_TRIANGLE:
            if (phase < M_PI) {
                return (2.0f * phase / M_PI) - 1.0f;  // 上升沿
            } else {
                return 3.0f - (2.0f * phase / M_PI);  // 下降沿
            }
            
        case SIGNAL_TYPE_SAWTOOTH:
            return (phase / M_PI) - 1.0f;
            
        default:
            return sinf(phase);  // 默认为正弦波
    }
}

/**
 * @brief 绘制信号参数信息
 * @param u8g2 U8G2显示器对象指针
 * @param signal 信号数据指针
 */
static void draw_signal_info(u8g2_t *u8g2, wave_t *signal)
{
    char info_str[32];
    
    // 设置小字体
    u8g2_SetFont(u8g2, u8g2_font_5x7_tf);
    
    // 显示频率
    if (signal->wave_freq >= 1000.0f) {
        snprintf(info_str, sizeof(info_str), "%.2fkHz", signal->wave_freq / 1000.0f);
    } else {
        snprintf(info_str, sizeof(info_str), "%.1fHz", signal->wave_freq);
    }
    u8g2_DrawStr(u8g2, 0, 8, info_str);
    
    // 显示峰峰值
    snprintf(info_str, sizeof(info_str), "%.2fVp-p", signal->wave_peak);
    u8g2_DrawStr(u8g2, 70, 8, info_str);
    
    // 显示波形类型
    const char* type_names[] = {"SINE", "SQR", "TRI", "SAW", "UNK"};
    u8g2_DrawStr(u8g2, 0, 63, type_names[signal->wave_type]);
    
    // 显示主频幅度
    snprintf(info_str, sizeof(info_str), "%d", time_set);
    u8g2_DrawStr(u8g2, 70, 63, info_str);
}

// ==================== 辅助函数 ====================

/**
 * @brief 设置显示区域参数
 * @param x_start X起始坐标
 * @param y_start Y起始坐标  
 * @param width 宽度
 * @param height 高度
 * @param grid_enabled 是否显示网格
 */
void signal_display_set_area(uint8_t x_start, uint8_t y_start, uint8_t width, uint8_t height, uint8_t grid_enabled)
{
    g_display_area.x_start = x_start;
    g_display_area.y_start = y_start;
    g_display_area.width = width;
    g_display_area.height = height;
    g_display_area.grid_enabled = grid_enabled;
}

/**
 * @brief 获取当前显示区域配置
 * @return 显示区域配置指针
 */
const display_area_t* signal_display_get_area(void)
{
    return &g_display_area;
}

// ==================== 使用示例 ====================

/*
使用方法示例：

#include "signal_display.h"

// 在主函数中
void main(void)
{
    u8g2_t u8g2;
    signal_data_t current_signal;
    
    // 初始化U8G2（根据你的硬件配置）
    // u8g2_Setup_ssd1306_i2c_128x64_noname_f(&u8g2, ...);
    // u8g2_InitDisplay(&u8g2);
    // u8g2_SetPowerSave(&u8g2, 0);
    
    // 初始化信号显示系统
    signal_display_init(&u8g2);
    
    while(1)
    {
        // 获取你的信号测量数据
        current_signal.frequency = get_measured_frequency();
        current_signal.peak_to_peak = get_measured_peak_to_peak();
        current_signal.main_freq = get_measured_main_freq();
        current_signal.main_amplitude = get_measured_main_amplitude();
        
        // 更新显示
        signal_display_update(&u8g2, &current_signal);
        
        // 延时
        HAL_Delay(100);
    }
}
*/




/**
 * @brief 绘制频谱图
 * @param u8g2 U8G2显示器对象指针
 * @param signal 信号数据指针
 */
/*void signal_display_spectrum(u8g2_t *u8g2, wave_t *signal)
{
    if (!signal || signal->wave_freq <= 0) {
        return;
    }
    
    // 清屏并开始绘制
    u8g2_ClearBuffer(u8g2);
    
    // 绘制频谱图边框和标题
    u8g2_SetFont(u8g2, u8g2_font_6x10_tf);
    u8g2_DrawStr(u8g2, 0, 10, "Spectrum");
    
    // 定义频谱显示区域
    uint8_t spec_x = 8;
    uint8_t spec_y = 16;
    uint8_t spec_width = 112;
    uint8_t spec_height = 40;
    
    // 绘制频谱图边框
    u8g2_DrawFrame(u8g2, spec_x, spec_y, spec_width, spec_height);
    
    // 计算频率范围和刻度
    float max_freq = signal->wave_freq * 8;  // 显示到8次谐波
    if (max_freq > 10000) max_freq = 10000;  // 限制在10kHz以内
    
    float freq_per_pixel = max_freq / spec_width;
    float max_amplitude = signal->wave_amplitude * 1.2f;  // 留20%余量
    float amp_per_pixel = max_amplitude / spec_height;
    
    // 绘制基频和谐波
    signal_type_t type = SIGNAL_TYPE_SINE;
    
    // 基频(1次谐波)
    uint8_t base_x = spec_x + (uint8_t)(signal->wave_freq / freq_per_pixel);
    uint8_t base_height = (uint8_t)(signal->wave_amplitude / amp_per_pixel);
    if (base_height > spec_height) base_height = spec_height;
    if (base_x < spec_x + spec_width) {
        u8g2_DrawVLine(u8g2, base_x, spec_y + spec_height - base_height, base_height);
        // 基频顶部加粗显示
        u8g2_DrawVLine(u8g2, base_x + 1, spec_y + spec_height - base_height, 2);
    }
    
    // 根据信号类型绘制谐波
    switch (type) {
        case SIGNAL_TYPE_SQUARE:
            // 方波：奇次谐波，幅度递减(1/n)
            for (uint8_t n = 3; n <= 7; n += 2) {
                float harmonic_freq = signal->wave_freq * n;
                if (harmonic_freq > max_freq) break;
                
                uint8_t harm_x = spec_x + (uint8_t)(harmonic_freq / freq_per_pixel);
                uint8_t harm_height = (uint8_t)((signal->wave_amplitude / n) / amp_per_pixel);
                if (harm_height > spec_height) harm_height = spec_height;
                if (harm_height < 2) harm_height = 2;  // 最小显示高度
                
                if (harm_x < spec_x + spec_width) {
                    u8g2_DrawVLine(u8g2, harm_x, spec_y + spec_height - harm_height, harm_height);
                }
            }
            break;
            
        case SIGNAL_TYPE_SAWTOOTH:
            // 锯齿波：所有谐波，幅度递减(1/n)
            for (uint8_t n = 2; n <= 8; n++) {
                float harmonic_freq = signal->wave_freq * n;
                if (harmonic_freq > max_freq) break;
                
                uint8_t harm_x = spec_x + (uint8_t)(harmonic_freq / freq_per_pixel);
                uint8_t harm_height = (uint8_t)((signal->wave_amplitude / n) / amp_per_pixel);
                if (harm_height > spec_height) harm_height = spec_height;
                if (harm_height < 1) harm_height = 1;
                
                if (harm_x < spec_x + spec_width) {
                    u8g2_DrawVLine(u8g2, harm_x, spec_y + spec_height - harm_height, harm_height);
                }
            }
            break;
            
        case SIGNAL_TYPE_TRIANGLE:
            // 三角波：奇次谐波，幅度递减(1/n²)
            for (uint8_t n = 3; n <= 7; n += 2) {
                float harmonic_freq = signal->wave_freq * n;
                if (harmonic_freq > max_freq) break;
                
                uint8_t harm_x = spec_x + (uint8_t)(harmonic_freq / freq_per_pixel);
                uint8_t harm_height = (uint8_t)((signal->wave_amplitude / (n * n)) / amp_per_pixel);
                if (harm_height > spec_height) harm_height = spec_height;
                if (harm_height < 1) harm_height = 1;
                
                if (harm_x < spec_x + spec_width) {
                    u8g2_DrawVLine(u8g2, harm_x, spec_y + spec_height - harm_height, harm_height);
                }
            }
            break;
            
        case SIGNAL_TYPE_SINE:
        default:
            // 正弦波：只有基频，可能有微弱的谐波噪声
            if (signal->wave_freq * 2 < max_freq) {
                uint8_t noise_x = spec_x + (uint8_t)((signal->wave_freq * 2) / freq_per_pixel);
                uint8_t noise_height = 1;  // 很小的噪声
                if (noise_x < spec_x + spec_width) {
                    u8g2_DrawPixel(u8g2, noise_x, spec_y + spec_height - noise_height);
                }
            }
            break;
    }
    
    // 绘制频率刻度标记
    u8g2_SetFont(u8g2, u8g2_font_4x6_tf);
    
    // 基频标记
    char freq_str[16];
    if (signal->wave_freq >= 1000) {
        snprintf(freq_str, sizeof(freq_str), "%.1fk", signal->wave_freq / 1000.0f);
    } else {
        snprintf(freq_str, sizeof(freq_str), "%.0f", signal->wave_freq);
    }
    u8g2_DrawStr(u8g2, base_x - 8, spec_y + spec_height + 8, freq_str);
    
    // 最大频率标记
    if (max_freq >= 1000) {
        snprintf(freq_str, sizeof(freq_str), "%.0fk", max_freq / 1000.0f);
    } else {
        snprintf(freq_str, sizeof(freq_str), "%.0f", max_freq);
    }
    u8g2_DrawStr(u8g2, spec_x + spec_width - 16, spec_y + spec_height + 8, freq_str);
    
    // 显示信号参数
    u8g2_SetFont(u8g2, u8g2_font_5x7_tf);
    const char* type_names[] = {"SINE", "SQR", "TRI", "SAW", "UNK"};
    u8g2_DrawStr(u8g2, 80, 10, type_names[type]);
    
    // 显示主频幅度
    //snprintf(freq_str, sizeof(freq_str), "%.2fV", signal->wave_amplitude);
    //u8g2_DrawStr(u8g2, 0, 63, freq_str);
    
    // 显示THD估算值（总谐波失真）
    float thd_estimate = 0.0f;
    switch (type) {
        case SIGNAL_TYPE_SQUARE: thd_estimate = 48.3f; break;  // 方波理论THD
        case SIGNAL_TYPE_SAWTOOTH: thd_estimate = 30.8f; break; // 锯齿波理论THD
        case SIGNAL_TYPE_TRIANGLE: thd_estimate = 12.1f; break; // 三角波理论THD
        case SIGNAL_TYPE_SINE: thd_estimate = 0.1f; break;     // 正弦波很低THD
        default: thd_estimate = 5.0f; break;
    }
    snprintf(freq_str, sizeof(freq_str), "THD:%.1f%%", thd_estimate);
    u8g2_DrawStr(u8g2, 60, 63, freq_str);
    
    // 发送到显示器
    u8g2_SendBuffer(u8g2);
}
*/
/**
 * @brief 绘制频谱图
 * @param u8g2 U8G2显示器对象指针
 * @param signal 信号数据指针
 * @param fft_data FFT频谱数据指针（可选，为NULL时显示理论谐波）
 * @param fft_size FFT数据点数
 * @param sample_rate 采样率
 */
void signal_display_spectrum(u8g2_t *u8g2, wave_t *signal, float *fft_data, uint16_t fft_size, float sample_rate)
{
    if (!signal || signal->wave_freq <= 0) {
        return;
    }
    
    // 清屏并开始绘制
    u8g2_ClearBuffer(u8g2);
    
    // 绘制频谱图边框和标题
    u8g2_SetFont(u8g2, u8g2_font_6x10_tf);
    u8g2_DrawStr(u8g2, 0, 10, "Spectrum");
    
    // 定义频谱显示区域
    uint8_t spec_x = 8;
    uint8_t spec_y = 16;
    uint8_t spec_width = 112;
    uint8_t spec_height = 40;
    
    // 绘制频谱图边框
    u8g2_DrawFrame(u8g2, spec_x, spec_y, spec_width, spec_height);
    
    // 计算频率范围和刻度
    float max_freq = signal->wave_freq * 8;  // 显示到8次谐波
    if (max_freq > 10000) max_freq = 10000;  // 限制在10kHz以内
    
    float freq_per_pixel = max_freq / spec_width;
    float max_amplitude = signal->wave_amplitude * 1.2f;  // 留20%余量
    float amp_per_pixel = max_amplitude / spec_height;
    
    // 如果有FFT数据，显示实际FFT频谱
    if (fft_data && fft_size > 0 && sample_rate > 0) {
        // 计算频率分辨率
        float freq_resolution = sample_rate / fft_size;
        
        // 找到FFT数据中的最大幅度用于归一化
        float fft_max_amp = 0.0f;
        uint16_t max_bin = (uint16_t)(max_freq / freq_resolution);
        if (max_bin > fft_size / 2) max_bin = fft_size / 2;
        
        for (uint16_t i = 1; i < max_bin; i++) {
            if (fft_data[i] > fft_max_amp) {
                fft_max_amp = fft_data[i];
            }
        }
        
        // 绘制FFT频谱
        if (fft_max_amp > 0) {
            for (uint16_t i = 1; i < max_bin; i++) {
                float freq = i * freq_resolution;
                if (freq > max_freq) break;
                
                uint8_t x_pos = spec_x + (uint8_t)(freq / freq_per_pixel);
                if (x_pos >= spec_x + spec_width) break;
                
                // 归一化FFT幅度到显示高度
                uint8_t line_height = (uint8_t)((fft_data[i] / fft_max_amp) * spec_height);
                if (line_height < 1 && fft_data[i] > fft_max_amp * 0.01f) {
                    line_height = 1;  // 保证小信号也能显示
                }
                
                if (line_height > 0) {
                    u8g2_DrawVLine(u8g2, x_pos, spec_y + spec_height - line_height, line_height);
                }
            }
        }
        
        // 高亮基频位置
        uint8_t base_x = spec_x + (uint8_t)(signal->wave_freq / freq_per_pixel);
        if (base_x < spec_x + spec_width) {
            // 在基频位置画一个标记
            u8g2_DrawPixel(u8g2, base_x, spec_y - 2);
            u8g2_DrawPixel(u8g2, base_x - 1, spec_y - 1);
            u8g2_DrawPixel(u8g2, base_x + 1, spec_y - 1);
        }
    } else {
        // 原有的理论谐波显示代码
        signal_type_t type = SIGNAL_TYPE_SINE;
        
        // 基频(1次谐波)
        uint8_t base_x = spec_x + (uint8_t)(signal->wave_freq / freq_per_pixel);
        uint8_t base_height = (uint8_t)(signal->wave_amplitude / amp_per_pixel);
        if (base_height > spec_height) base_height = spec_height;
        if (base_x < spec_x + spec_width) {
            u8g2_DrawVLine(u8g2, base_x, spec_y + spec_height - base_height, base_height);
            // 基频顶部加粗显示
            u8g2_DrawVLine(u8g2, base_x + 1, spec_y + spec_height - base_height, 2);
        }
        
        // 根据信号类型绘制谐波
        switch (type) {
            case SIGNAL_TYPE_SQUARE:
                // 方波：奇次谐波，幅度递减(1/n)
                for (uint8_t n = 3; n <= 7; n += 2) {
                    float harmonic_freq = signal->wave_freq * n;
                    if (harmonic_freq > max_freq) break;
                    
                    uint8_t harm_x = spec_x + (uint8_t)(harmonic_freq / freq_per_pixel);
                    uint8_t harm_height = (uint8_t)((signal->wave_amplitude / n) / amp_per_pixel);
                    if (harm_height > spec_height) harm_height = spec_height;
                    if (harm_height < 2) harm_height = 2;  // 最小显示高度
                    
                    if (harm_x < spec_x + spec_width) {
                        u8g2_DrawVLine(u8g2, harm_x, spec_y + spec_height - harm_height, harm_height);
                    }
                }
                break;
                
            case SIGNAL_TYPE_SAWTOOTH:
                // 锯齿波：所有谐波，幅度递减(1/n)
                for (uint8_t n = 2; n <= 8; n++) {
                    float harmonic_freq = signal->wave_freq * n;
                    if (harmonic_freq > max_freq) break;
                    
                    uint8_t harm_x = spec_x + (uint8_t)(harmonic_freq / freq_per_pixel);
                    uint8_t harm_height = (uint8_t)((signal->wave_amplitude / n) / amp_per_pixel);
                    if (harm_height > spec_height) harm_height = spec_height;
                    if (harm_height < 1) harm_height = 1;
                    
                    if (harm_x < spec_x + spec_width) {
                        u8g2_DrawVLine(u8g2, harm_x, spec_y + spec_height - harm_height, harm_height);
                    }
                }
                break;
                
            case SIGNAL_TYPE_TRIANGLE:
                // 三角波：奇次谐波，幅度递减(1/n²)
                for (uint8_t n = 3; n <= 7; n += 2) {
                    float harmonic_freq = signal->wave_freq * n;
                    if (harmonic_freq > max_freq) break;
                    
                    uint8_t harm_x = spec_x + (uint8_t)(harmonic_freq / freq_per_pixel);
                    uint8_t harm_height = (uint8_t)((signal->wave_amplitude / (n * n)) / amp_per_pixel);
                    if (harm_height > spec_height) harm_height = spec_height;
                    if (harm_height < 1) harm_height = 1;
                    
                    if (harm_x < spec_x + spec_width) {
                        u8g2_DrawVLine(u8g2, harm_x, spec_y + spec_height - harm_height, harm_height);
                    }
                }
                break;
                
            case SIGNAL_TYPE_SINE:
            default:
                // 正弦波：只有基频，可能有微弱的谐波噪声
                if (signal->wave_freq * 2 < max_freq) {
                    uint8_t noise_x = spec_x + (uint8_t)((signal->wave_freq * 2) / freq_per_pixel);
                    uint8_t noise_height = 1;  // 很小的噪声
                    if (noise_x < spec_x + spec_width) {
                        u8g2_DrawPixel(u8g2, noise_x, spec_y + spec_height - noise_height);
                    }
                }
                break;
        }
    }
    
    // 绘制频率刻度标记
    u8g2_SetFont(u8g2, u8g2_font_4x6_tf);
    
    // 基频标记
    char freq_str[16];
    uint8_t base_x = spec_x + (uint8_t)(signal->wave_freq / freq_per_pixel);
    if (signal->wave_freq >= 1000) {
        snprintf(freq_str, sizeof(freq_str), "%.1fk", signal->wave_freq / 1000.0f);
    } else {
        snprintf(freq_str, sizeof(freq_str), "%.0f", signal->wave_freq);
    }
    u8g2_DrawStr(u8g2, base_x - 8, spec_y + spec_height + 8, freq_str);
    
    // 最大频率标记
    if (max_freq >= 1000) {
        snprintf(freq_str, sizeof(freq_str), "%.0fk", max_freq / 1000.0f);
    } else {
        snprintf(freq_str, sizeof(freq_str), "%.0f", max_freq);
    }
    u8g2_DrawStr(u8g2, spec_x + spec_width - 16, spec_y + spec_height + 8, freq_str);
    
    // 显示信号参数
    u8g2_SetFont(u8g2, u8g2_font_5x7_tf);
    const char* type_names[] = {"SINE", "SQR", "TRI", "SAW", "UNK"};
    signal_type_t type = SIGNAL_TYPE_SINE;
    u8g2_DrawStr(u8g2, 80, 10, type_names[type]);
    
    // 显示数据源标识
    if (fft_data) {
        u8g2_DrawStr(u8g2, 0, 63, "FFT");
    } else {
        u8g2_DrawStr(u8g2, 0, 63, "SIM");  // 模拟谐波
    }
    
    // 显示主频幅度
    //snprintf(freq_str, sizeof(freq_str), "%.2fV", signal->wave_amplitude);
    //u8g2_DrawStr(u8g2, 0, 63, freq_str);
    
    // 显示THD估算值（总谐波失真）
    float thd_estimate = 0.0f;
    if (fft_data) {
        // 如果有FFT数据，可以计算实际THD
        thd_estimate = 0.0f;  // 这里可以添加实际THD计算
        snprintf(freq_str, sizeof(freq_str), "THD:--%%");
    } else {
        // 理论THD值
        switch (type) {
            case SIGNAL_TYPE_SQUARE: thd_estimate = 48.3f; break;  // 方波理论THD
            case SIGNAL_TYPE_SAWTOOTH: thd_estimate = 30.8f; break; // 锯齿波理论THD
            case SIGNAL_TYPE_TRIANGLE: thd_estimate = 12.1f; break; // 三角波理论THD
            case SIGNAL_TYPE_SINE: thd_estimate = 0.1f; break;     // 正弦波很低THD
            default: thd_estimate = 5.0f; break;
        }
        snprintf(freq_str, sizeof(freq_str), "THD:%.1f%%", thd_estimate);
    }
    u8g2_DrawStr(u8g2, 60, 63, freq_str);
    
    // 发送到显示器
    u8g2_SendBuffer(u8g2);
}